package top.kaoshanji.q1ch05a.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * 代码清单-q1ch05a-141页a：最小但可用的Spring MVC配置
 * @author kaoshanji
 * ^_^
 * create time 2025/4/23 19:59
 */
@Configuration
@EnableWebMvc // 启用 Spring MVC
@ComponentScan("top.kaoshanji.q1ch05a.web") // 启用组件扫描
public class WebConfig extends WebMvcConfigurerAdapter {

    // 定义一个JSP视图解析器的bean
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/"); // 需要以 / 结尾
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);

        return resolver;
    }

    // 覆盖父类里的方法：配置静态资源的处理
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}
